package com.wangyb.wework.service.medium;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.wangyb.wework.bo.WwMediumAddBo;
import com.wangyb.wework.bo.WwMediumEditBo;
import com.wangyb.wework.bo.WwMediumQueryBo;
import com.wangyb.wework.entity.medium.WwMedium;
import com.wangyb.wework.mapper.medium.WwMediumMapper;
import com.wangyb.wework.vo.WwMediumVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 素材库 Service业务层处理
 *
 * @author wangyb
 * @date 2021-07-27
 */
@Service
public class WwMediumServiceImpl extends ServiceImpl<WwMediumMapper, WwMedium> implements IWwMediumService {
	@Override
	public WwMediumVo queryById(Long id){
		return getVoById(id, WwMediumVo.class);
	}

	@Override
	public TableDataInfo<WwMediumVo> queryPageList(WwMediumQueryBo bo) {
		PagePlus<WwMedium, WwMediumVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), WwMediumVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<WwMediumVo> queryList(WwMediumQueryBo bo) {
		return listVo(buildQueryWrapper(bo), WwMediumVo.class);
	}

	private LambdaQueryWrapper<WwMedium> buildQueryWrapper(WwMediumQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<WwMedium> lqw = Wrappers.lambdaQuery();
		lqw.eq(StringUtils.isNotBlank(bo.getMediaId()), WwMedium::getMediaId, bo.getMediaId());
		lqw.eq(bo.getLastUploadTime() != null, WwMedium::getLastUploadTime, bo.getLastUploadTime());
		lqw.eq(bo.getType() != null, WwMedium::getType, bo.getType());
		lqw.eq(bo.getIsSync() != null, WwMedium::getIsSync, bo.getIsSync());
		lqw.eq(StringUtils.isNotBlank(bo.getContent()), WwMedium::getContent, bo.getContent());
		lqw.eq(StringUtils.isNotBlank(bo.getTags()), WwMedium::getTags, bo.getTags());
		lqw.eq(bo.getCorpId() != null, WwMedium::getCorpId, bo.getCorpId());
		lqw.eq(bo.getUserId() != null, WwMedium::getUserId, bo.getUserId());
		lqw.like(StringUtils.isNotBlank(bo.getUserName()), WwMedium::getUserName, bo.getUserName());
		lqw.eq(bo.getCreateTime() != null, WwMedium::getCreateTime, bo.getCreateTime());
		lqw.eq(bo.getUpdateTime() != null, WwMedium::getUpdateTime, bo.getUpdateTime());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(WwMediumAddBo bo) {
		WwMedium add = BeanUtil.toBean(bo, WwMedium.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(WwMediumEditBo bo) {
		WwMedium update = BeanUtil.toBean(bo, WwMedium.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(WwMedium entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
